const Service = require('../../../core/service/ApiService');
const {Transactional} = require('../../../../plugin/egg-sequelize-transactional');

class RecordSvc extends Service {
    //借用记录
    async getRecord(user_id) {
        return this.service.miniapp.record.dao.getRecord(user_id);
    }

    //借用排行
    async getRecordRunk() {
        return {
            record_runk: await this.service.miniapp.record.dao.getRecordRunk()
        }
    }

    //个人借用记录排名
    async personal(open) {
        return {
            record_runk: await this.service.miniapp.record.dao.personal(open)
        }
    }

    //设备借出
    // @Transactional()
    async addRecord(info) {
        info.nowDate =this.helper.YMDhms(new Date());
        info.status = this.constant.EMPLOY_STATUS.SIGNALED;
        return this.service.miniapp.record.dao.addRecord(info)
    }

    //提交
    @Transactional()
    async commit(u_info) {
        const dao = this.service.miniapp.record.dao;
        const record = await dao.getRecordInfo(u_info.device_id,u_info.user_id);
        if(!record)
            throw new this.error.BusinessError('不存在该记录');
        const ps = this.helper.makePromises();
        ps
            .push(dao.commit(u_info))
            .push(dao.updateDeviceStatus(u_info));
        await ps.execute();

    }

    //延迟
    async delay(delay_info) {
        delay_info.date = new Date();
        const info = await this.service.miniapp.record.dao.getRecordInfoFromDb(delay_info.record_id);
        if(!info)
        {
            throw new this.error.BusinessError('该用户已延迟一次');
        }
        return this.service.miniapp.record.dao.delay(delay_info);
    }
}

module.exports = RecordSvc;
